package zhongbo.java.air.monitor.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/websocket/alert")
public class AlertZoneWebSocket {

    public static final ConcurrentHashMap<String, Session> SessionMap = new ConcurrentHashMap<>();


    @OnOpen
    public void onOpen(Session session) {
        System.out.println("连接成功!");
        SessionMap.put(session.getId(),session);
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("收到消息" + message);
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("连接已关闭!");
        SessionMap.remove(session.getId());
    }
}
